package data {
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import utils.Utilities;
	
	/**
	 * Class containing apartment information.
	 * It is used with apartment MovieClip.
	 * 
	 * @author Vjekoslav Ratkajec
	 */
	public class Apartment extends EventDispatcher{
		
		private var graphicsLoadedCount = 0;
		
		private var _id:int;
		private var _code:String;
		private var _building:String;
		private var _state:String;
		private var _floor:String;
		private var _numRooms:String;
		private var _area:String;
		private var _price:Number;
		private var _layoutPath:String;
		
		private var _layout:MovieClip = new MovieClip();
		private var _layoutThumb:MovieClip = new MovieClip();
		private var _topview:MovieClip = new MovieClip();
		private var _topviewThumb:MovieClip = new MovieClip();
		private var _table:MovieClip = new MovieClip();
		
		public function Apartment(id:int) {
			_id = id;
			init();
		}
		
		private function init():void {
			loadApartmentXML();
		}
		
		public function loadApartmentXML():void {
			var url:String = "http://www.ban-centar.hr/xml/Apartment/" + id;
			try {
				Utilities.loadURL(url, loadApartmentXML_Complete);
			} catch (e:Error) {
				//If error occurs, display it and set floor as empty, so no data will be fetched and shown
				trace(e);
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		private function loadApartmentXML_Complete(event:Event):void {
			var xml:XML = XML(event.target.data);
			
			if (xml.children().length() > 0) {
				_code = String(xml.code);
				_building = String(xml.building);
				_state = String(xml.state);
				_floor = String(xml.floor);
				_numRooms = String(xml.roomsnumber);
				_area = Number(xml.area).toFixed(2);
				_price = Number(xml.price);
				
				var graphics:XML = XML(xml.graphics);
				_layoutPath = "http://" + String(graphics.layout);
				var layoutURL:String = "http://" + String(graphics.layout);
				var layoutThumbURL:String = "http://" + String(graphics.layoutthumb);
				var topviewURL:String = "http://" + String(graphics.topview);
				var topviewThumbURL:String = "http://" + String(graphics.topviewthumb);
				var rawTable:String = String(graphics.table).replace(".png", "");
				var tableChar:String = String(graphics.table).charAt(String(graphics.table).length - 5);
				var tableURL:String;
				if (tableChar == "a" || tableChar == "b") {
					tableURL = "http://" + String(graphics.table).slice(0, String(graphics.table).length - 5) + 
								String(graphics.table).slice(String(graphics.table).length - 4)
				} else {
					tableURL = "http://" + String(graphics.table);
				}
				
				Utilities.loadContent(layoutURL, layout_Complete);
				Utilities.loadContent(layoutThumbURL, layoutThumb_Complete);
				Utilities.loadContent(topviewURL, topview_Complete);
				Utilities.loadContent(topviewThumbURL, topviewThumb_Complete);
				Utilities.loadContent(tableURL, table_Complete);				
			} else {
				throw new Error("Error reading apartment from database");
			}
		}
		
		private function layout_Complete(event:Event):void {
			var loader:Loader = Loader(event.currentTarget.loader);
			_layout.addChild(loader.content);
			graphicsLoadedCount++;
			checkIfComplete();
		}
		
		private function layoutThumb_Complete(event:Event):void {
			var loader:Loader = Loader(event.currentTarget.loader);
			_layoutThumb.addChild(loader.content);
			graphicsLoadedCount++;
			checkIfComplete();
		}
		
		private function topview_Complete(event:Event):void {
			var loader:Loader = Loader(event.currentTarget.loader);
			_topview.addChild(loader.content);
			graphicsLoadedCount++;
			checkIfComplete();
		}
		
		private function topviewThumb_Complete(event:Event):void {
			var loader:Loader = Loader(event.currentTarget.loader);
			_topviewThumb.addChild(loader.content);
			graphicsLoadedCount++;
			checkIfComplete();
		}
		
		private function table_Complete(event:Event):void {
			var loader:Loader = Loader(event.currentTarget.loader);
			_table.addChild(loader.content);
			graphicsLoadedCount++;
			checkIfComplete();
		}
		
		private function checkIfComplete():void {
			if (graphicsLoadedCount >= 5) {
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		//{ region Getters
		
		public function get id():int {
			return _id;
		}
		
		public function get code():String {
			return _code;
		}
		
		public function get building():String {
			return _building;
		}
		
		public function get state():String {
			return _state;
		}
		
		public function get floor():String {
			return _floor;
		}
		
		public function get numRooms():String {
			return _numRooms;
		}
		
		public function get area():String {
			return _area;
		}
		
		public function get price():Number {
			return _price;
		}
		
		public function get layoutPath():String {
			return _layoutPath;
		}
		
		public function get layout():MovieClip {
			return _layout;
		}
		
		public function get layoutThumb():MovieClip {
			return _layoutThumb;
		}
		
		public function get topview():MovieClip {
			return _topview;
		}
		
		public function get topviewThumb():MovieClip {
			return _topviewThumb;
		}
		
		public function get table():MovieClip {
			return _table;
		}
		
		//} endregion
		
	}

}